package com.ly.thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Phone {

    private Lock lock = new ReentrantLock();

    private static Lock lock2 = new ReentrantLock();

    public void sms() {
        lock.lock();
        try{

            System.out.println("sendSms");
        }catch(Exception e){

        }finally {
            lock.unlock();
        }
    }

    public void sendEmail() {
        lock.lock();
        try{
            TimeUnit.SECONDS.sleep(3);
            System.out.println("sendEmail");
        }catch(Exception e){

        }finally {
            lock.unlock();
        }
    }

    public void f1() {
        System.out.println("f1");
    }


    public static void sms2() {
        lock2.lock();
        try{
            System.out.println("sendSms");
        }catch(Exception e){

        }finally {
            lock2.unlock();
        }
    }

    public static void sendEmail2() {
        lock2.lock();
        try{
            TimeUnit.SECONDS.sleep(3);
            System.out.println("sendEmail");
        }catch(Exception e){

        }finally {
            lock2.unlock();
        }
    }
}


public class Lock8 {
    public static void main(String[] args) throws InterruptedException {
        Phone phone = new Phone();
        Phone phone2 = new Phone();
//        1.txt.正常访问   e s
//        new Thread(() -> {phone.sendEmail();},"A").start();
//        TimeUnit.MILLISECONDS.sleep(200);
//        new Thread(() -> {phone.sms();},"B").start();
//        2.邮件暂停4秒   e s
//        new Thread(() -> {phone.sendEmail();},"A").start();
//        TimeUnit.MILLISECONDS.sleep(200);
//        new Thread(() -> {phone.sms();},"B").start();
//        3.调用非锁方法  f s
//        new Thread(() -> {phone.sendEmail();},"A").start();
//        TimeUnit.MILLISECONDS.sleep(200);
//        new Thread(() -> {phone.f1();},"B").start();
//        4.两部手机   s - e
//        new Thread(() -> {phone.sendEmail();},"A").start();
//        TimeUnit.MILLISECONDS.sleep(200);
//        new Thread(() -> {phone2.sms();},"B").start();
//        5.两个静态同步方法
//        new Thread(() -> {phone.sendEmail2();},"A").start();
//        TimeUnit.MILLISECONDS.sleep(200);
//        new Thread(() -> {phone.sms2();},"B").start();
//        6.两个静态同步方法，两部手机
//        new Thread(() -> {phone.sendEmail2();},"A").start();
//        TimeUnit.MILLISECONDS.sleep(200);
//        new Thread(() -> {phone2.sms2();},"B").start();
//        7.1个普通同步，一个静态同步,一个手机
//        new Thread(() -> {phone.sendEmail();},"A").start();
//        TimeUnit.MILLISECONDS.sleep(200);
//        new Thread(() -> {phone.sms2();},"B").start();
//        8.1个普通同步，一个静态同步,一个手机，两部手机
        new Thread(() -> {phone.sendEmail();},"A").start();
        TimeUnit.MILLISECONDS.sleep(200);
        new Thread(() -> {phone2.sms2();},"B").start();
    }
}
